package com.nanhai.buddhist.college.module.system.controller.admin.rolegroup;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.*;
import com.nanhai.buddhist.college.module.system.dal.dataobject.rolegroup.RoleGroupUserDO;
import com.nanhai.buddhist.college.module.system.convert.rolegroup.RoleGroupUserConvert;
import com.nanhai.buddhist.college.module.system.service.rolegroup.RoleGroupUserService;

@Api(tags = "管理后台 - 角色群组用户")
@RestController
@RequestMapping("/system/role-group-user")
@Validated
public class RoleGroupUserController {

    @Resource
    private RoleGroupUserService roleGroupUserService;

    @PostMapping("/create")
    @ApiOperation("创建角色群组用户")
    @PreAuthorize("@ss.hasPermission('system:role-group-user:create')")
    public CommonResult<Long> createRoleGroupUser(@Valid @RequestBody RoleGroupUserCreateReqVO createReqVO) {
        return success(roleGroupUserService.createRoleGroupUser(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新角色群组用户")
    @PreAuthorize("@ss.hasPermission('system:role-group-user:update')")
    public CommonResult<Boolean> updateRoleGroupUser(@Valid @RequestBody RoleGroupUserUpdateReqVO updateReqVO) {
        roleGroupUserService.updateRoleGroupUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除角色群组用户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:role-group-user:delete')")
    public CommonResult<Boolean> deleteRoleGroupUser(@RequestParam("id") Long id) {
        roleGroupUserService.deleteRoleGroupUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得角色群组用户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:role-group-user:query')")
    public CommonResult<RoleGroupUserRespVO> getRoleGroupUser(@RequestParam("id") Long id) {
        RoleGroupUserDO roleGroupUser = roleGroupUserService.getRoleGroupUser(id);
        return success(RoleGroupUserConvert.INSTANCE.convert(roleGroupUser));
    }

    @GetMapping("/list")
    @ApiOperation("获得角色群组用户列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:role-group-user:query')")
    public CommonResult<List<RoleGroupUserRespVO>> getRoleGroupUserList(@RequestParam("ids") Collection<Long> ids) {
        List<RoleGroupUserDO> list = roleGroupUserService.getRoleGroupUserList(ids);
        return success(RoleGroupUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得角色群组用户分页")
    @PreAuthorize("@ss.hasPermission('system:role-group-user:query')")
    public CommonResult<PageResult<RoleGroupUserRespVO>> getRoleGroupUserPage(@Valid RoleGroupUserPageReqVO pageVO) {
        PageResult<RoleGroupUserDO> pageResult = roleGroupUserService.getRoleGroupUserPage(pageVO);
        return success(RoleGroupUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出角色群组用户 Excel")
    @PreAuthorize("@ss.hasPermission('system:role-group-user:export')")
    @OperateLog(type = EXPORT)
    public void exportRoleGroupUserExcel(@Valid RoleGroupUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RoleGroupUserDO> list = roleGroupUserService.getRoleGroupUserList(exportReqVO);
        // 导出 Excel
        List<RoleGroupUserExcelVO> datas = RoleGroupUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "角色群组用户.xls", "数据", RoleGroupUserExcelVO.class, datas);
    }

}
